

$(function(){
	$('#tab').tabs({
		onSelect:function(title){
			console.log(title+' is selected');
			if ('权限' === title) {
				//
				
				
				$.get("/static/my/tree_data1.json", function(_data){
					console.log("====", _data);
					$('#tt2').tree({
						//url: ...,
						animate: true,
						checkbox: true,
						data: _data
					});
					console.log("====", "xxx");
				});
				$.get("/static/my/get_users.json", function(_data){
					console.log("====", _data);
					//$('#dg').datagrid('loadData', _data);
					$('#dg2').datagrid({
						data: _data,
						pageList: [2,5,10,15,20],
						pageSize: 2
					});
				});


				// 展开和折叠
				$('#btnOpen2').bind('click', function(){
					var rootNode = $('#tt2').tree('getRoot');
					//expandAll
					$('#tt2').tree('expandAll', rootNode.target);
				});
				$('#btnFold2').bind('click', function(){
					var rootNode = $('#tt2').tree('getRoot');
					//expandAll
					$('#tt2').tree('collapseAll', rootNode.target);
				});
				
				
			}
		}
	});
});



var url_2;
function newUser(){
	$('#dlg2').dialog('open').dialog('center').dialog('setTitle','New User');
	$('#fm2').form('clear');
	url_2 = 'save_user.php';
}
function editUser(){
	var row = $('#dg2').datagrid('getSelected');
	if (row){
		$('#dlg2').dialog('open').dialog('center').dialog('setTitle','Edit User');
		$('#fm2').form('load',row);
		url_2 = 'update_user.php?id='+row.id;
	}
}
function saveUser(){
	$('#fm2').form('submit',{
		url: url_2,
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			var result = eval('('+result+')');
			if (result.errorMsg){
				$.messager.show({
					title: 'Error',
					msg: result.errorMsg
				});
			} else {
				$('#dlg2').dialog('close');		// close the dialog
				$('#dg2').datagrid('reload');	// reload the user data
			}
		}
	});
}
function destroyUser(){
	var row = $('#dg2').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
			if (r){
				$.post('destroy_user.php',{id:row.id},function(result){
					if (result.success){
						$('#dg2').datagrid('reload');	// reload the user data
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.errorMsg
						});
					}
				},'json');
			}
		});
	}
}
